<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
	<title>title</title>
	<link rel="stylesheet" type="text/css" href="../../css/api.css" />
	<link rel="stylesheet" type="text/css" href="./css/login.css" />
</head>
<body>
	<div class="login_content_style">
		<div class="login_banner">
			<div class="login_back_icon" onclick="api.closeWin()">
				<img src="./image/login_back_icon.png" />
			</div>
			<img src="./image/login_banner.png" />
			<div class="login_top_bg"></div>
		</div>
		<div class="login_input_content">
			<div class="login_style_font">绑定手机号</div>
			<div class="login_input_btn">
				<div class="login_phoen_style">
					<img src="./image/login_phone.png" />
					<input type="number" placeholder="请输入您的手机号" id="mobile" class="login_phone mobile">
				</div>
				<div class="login_phoen_style">
					<div class="login_seat"></div>
					<input type="number" placeholder="请输入验证码" id="mobile_captcha" class="login_code">
					<input type="button" class="login_btn_code" value="获取验证码" onclick="settime(this)" />
				</div>
			</div>
		</div>
		<div class="login_btn" onclick="bindPhone()">
			<span id="login_btn_txt">绑定</span>
		</div>
	</div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/config.js"></script>
<script type="text/javascript" src="../../script/zepto.min.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript">
	var isinerval, times, smsVerify, pageParam;
	apiready = function() {
		smsVerify = api.require('smssdk');
		pageParam=api.pageParam.data;
		console.log(JSON.stringify(pageParam));
	};
	//发送验证码
	var countdown = 60;

	function settime(val) {
		var mobile = $("#mobile").val();
		if (mobile == "") {
			api.toast({
				msg: '请输入手机号!',
				duration: 2000,
				location: 'middle'
			});
			return false;
		}
		if (!(/^1[3456789]\d{9}$/.test(mobile))) {
			api.toast({
				msg: '手机号格式不正确，请重填!',
				duration: 2000,
				location: 'middle'
			});
			return false;
		}
		if (countdown == 0) {
			$(".login_btn_code").removeClass("active");
			val.removeAttribute("disabled");
			val.value = "获取验证码";
			countdown = 60;
			return;
		} else {
			$(".login_btn_code").addClass("active");
			val.setAttribute("disabled", true);
			val.value = "重新发送(" + countdown + ")";
			if (countdown == 60) {
				sendsmsfrommob(mobile);
			}
			countdown = parseInt(countdown) - 1;
		}
		setTimeout(function() {
			settime(val)
		}, 1000)
	}

	//发送短信
	function sendsmsfrommob(phone) {
		smsVerify.getTextCode({
			phoneNumber: phone,
			zone:'86'
		}, function(ret, err) {
				if(err !== null && err !== undefined && err !== ''){
					showToast(err.msg);
				}else{
					api.toast({
						msg: '验证码已发送,请注意查收!',
						duration: 2000,
						location: 'middle'
					});
				}
		});
	}

	//验证短信
	function yzsms(phone, code) {
		smsVerify.commitCode({
			phoneNumber: phone,
			zone:'86',
			code: code,
		}, function(ret, err) {
			console.log(JSON.stringify(ret));
				console.log(JSON.stringify(err));
			if (err !== null && err !== undefined && err !== '') {
					showToast(' 验证码不正确~');
			} else {
				showProgress('绑定中');
        dobindPhone(phone);
			}
		});
	}
	//登录
	function bindPhone() {
		var mobile = $("#mobile").val();
		var mobile_captcha = $("#mobile_captcha").val();
		if (mobile == "") {
			api.toast({
				msg: '请输入手机号!',
				duration: 2000,
				location: 'middle'
			});
			return false;
		}
		if (mobile_captcha == "") {
			api.toast({
				msg: '请输入验证码!',
				duration: 2000,
				location: 'middle'
			});
			return false;
		}
		if (!(/^1[3456789]\d{9}$/.test(mobile))) {
			api.toast({
				msg: '手机号格式不正确，请重填!',
				duration: 2000,
				location: 'middle'
			});
			return false;
		}
		yzsms(mobile, mobile_captcha);
	}
	function dobindPhone(mobile){
		var data=pageParam.param;
		data.mobile=mobile;
		console.log(JSON.stringify(data));
		postData(_URL + 'Login/bindphone',data,function(ret,err){
			console.log(JSON.stringify(ret));
			console.log(JSON.stringify(err));
			hideProgress();
			if(ret.code==1 && ret.logined==1){
				$api.setStorage('taobao_user', {
					'mobile': ret.data.mobile,
					'level_name': ret.data.level_name,
					'islogin': true,
					'yjrate': ret.data.yjrate,
					'xjyjrate': ret.data.xjyjrate,
					'id': ret.data.userid,
					'relation_id': ret.data.relation_id
				});
				var jsfun = 'location.reload();';
					api.execScript({
						name: 'root',
						frameName: 'my_win',
						script: jsfun
					});
				api.closeToWin({
				    name: 'root'
				});

			}else{
				showToast(showTips.data_busy);
			}
		})
	}
</script>

</html>
